<?php 

namespace Admin\Controller;


use Think\Upload;
use Think\Image;

class GoodsPhotoController extends BaseController{
    public function photoList(){
        $photo_M = M('goods_photo');
        // 获取商品id
        $goods_id = I('get.goods_id');
        
        // 获取商品图片数据
        $goods_photo = $photo_M->where(array('goods_id'=>$goods_id))->select();
        
        
        $this->assign('count',count($goods_photo));
        $this->assign('goods_photo',$goods_photo);
       $this->display();
   }
   
   // 添加图片
   public function photoAdd(){
       if(IS_POST){
           
           $goods_id = I('post.goods_id');
           $sort = I('post.sort',0);
           
           if (!is_numeric($sort)){
               $this->ajaxReturn(array('status'=>0,'msg'=>'排序请填写整数'));
           }
           
           // 获取上传图片
           if (!empty($_FILES)){
               $upload = new Upload();
               
               $upload->maxSize = 1024*1024*2;
               $upload->exts = array('jpg','png','jpeg','gif');
               $rootPath = 'Uploads';
               if (!file_exists($rootPath)){
                   mkdir($rootPath);
               }
               // 设置文件上传路径
               
               $info = $upload->upload();
               if (!$info){
                   
                   $this->ajaxReturn(array('status'=>0,'msg'=>$upload->getError()));
               }
               
               // 获取文件上传路径
               $savePath = $rootPath.'/'.$info['logo']['savepath'].$info['logo']['savename'];
               
               // 图片名
               $imageName = $info['logo']['savename'];
               // 大图路径
               $bigImagePath = $rootPath.'/'.$info['logo']['savepath'].'big_'.$imageName;
               // 中图
               $midImagePath = $rootPath.'/'.$info['logo']['savepath'].'mid_'.$imageName;
               // 小图路径
               $thumbImagePath = $rootPath.'/'.$info['logo']['savepath'].'thumb_'.$imageName;
               
               // 生成缩略图
               $image = new Image();
               $image->open($savePath);
               $image->thumb(300, 300,Image::IMAGE_THUMB_FILLED)->save($bigImagePath);
               $image->thumb(200, 200,Image::IMAGE_THUMB_FILLED)->save($midImagePath);
               $image->thumb(100, 100,Image::IMAGE_THUMB_FILLED)->save($thumbImagePath);
               
               $data = array(
                   'big_image' => '/'.$bigImagePath,
                   'mid_image' => '/'.$midImagePath,
                   'thumb_image' => '/'.$thumbImagePath,
                   'image' => '/'.$savePath,
                   'goods_id' => $goods_id,
                   'sort' => $sort
               );
           }else {
               $data = array(
                   'sort' => $sort
               );
           }
           $photo_id = I('post.photo_id');
           if ($photo_id > 0){
               $photo = M('goods_photo')->find($photo_id);
               $res = M('goods_photo')->where(array('id'=>$photo_id))->save($data);
               if ($res){
                   // 删除原来的图片
                   @unlink(trim($photo['image'],'/'));
                   @unlink(trim($photo['big_image'],'/'));
                   @unlink(trim($photo['mid_image'],'/'));
                   @unlink(trim($photo['thumb_image'],'/'));
               }
           }else{
               $res = M('goods_photo')->add($data);
           }
           
           if ($res !== false){
               $this->ajaxReturn(array('status'=>1));
           }else {
               $this->ajaxReturn(array('status'=>0,'msg'=>'失败，请重试'));
           }
       }elseif(I('get.photo_id')>0){
           $photo_id = I('get.photo_id');
           
           // 获取图片数据
           $photo = M('goods_photo')->where(array('id'=>$photo_id))->field('id,sort,image')->find();
         
           $this->assign('photo',$photo);
           $this->display();
       }else {
           $this->display();
       }
   }
    
   // 删除图片
   public function photoDel(){
       
       $photo_M = M('goods_photo');
       
       // 获取图片id
       $photo_id = I('get.photo_id',0);
       
       if($photo_id == 0){
           $photo_id = trim(I('post.id'),',');
           
           if ($photo_id == ''){
               $this->ajaxReturn(array('status'=>2,'msg'=>'请选择要删除的图片'));
           }
           
           // 获取所有的图片
           $ids = explode(',', $photo_id);
           $photos = $photo_M->where(array('id'=>array('in',$ids)))->select();
           
           // 删除数据
           $res = $photo_M->delete($photo_id);
           if ($res !== false){
               // 删除图片
               foreach ($photos as $v){
                   @unlink(trim($v['image'],'/'));
                   @unlink(trim($v['big_image'],'/'));
                   @unlink(trim($v['mid_image'],'/'));
                   @unlink(trim($v['thumb_image'],'/'));
               }
           
               $this->ajaxReturn(array('status'=>1));
           }else {
               $this->ajaxReturn(array('status'=>0,'msg'=>'删除失败'));
           }
       }else {
           // 获取所有图片
           $photo = $photo_M->find($photo_id);
            
           $res = $photo_M->delete($photo_id);
            
           if ($res !== false){
               // 删除图片
               @unlink(trim($photo['image'],'/'));
               @unlink(trim($photo['big_image'],'/'));
               @unlink(trim($photo['mid_image'],'/'));
               @unlink(trim($photo['thumb_image'],'/'));
                
               echo 1;die;
           }else {
               echo 0;die;
           }
       }
   }
}

?>